<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>Spring Boot之文件上传与下载 - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2019-06-30 21:22">
      June 30, 2019 pm
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.9k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      26
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h2 id="学习笔记-Spring-Boot之文件上传"><a href="#学习笔记-Spring-Boot之文件上传" class="headerlink" title="学习笔记 : Spring Boot之文件上传"></a>学习笔记 : Spring Boot之文件上传</h2><p><em>关键字 : <code>Servlet 3.0</code> 、 <code>SpringMVC</code> 、 <code>Spring Boot</code></em></p>
<h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p><em>前天使用Sprint Boot开发了一个基于SSM框架的项目,一个简单的好友备忘录,该项目地址 : <a href="https://github.com/YUbuntu0109/SpringBoot-CURD-Memo" target="_blank" rel="noopener">https://github.com/YUbuntu0109/SpringBoot-CURD-Memo</a> ,在该项目中除了基本的CURD,还添加了上传用户头像的功能哟~ 以至于在写此功能时发现了Spring Boot在上传文件时不同于<code>Spring MVC</code>的一个细节问题 : <code>头像被上传到非预期路径下!</code></em></p>
<ul>
<li><em>注 : Spring Boot启动时会创建一个<code>/tmp/tomcat.xxxxxx/work/Tomcat/localhost/ROOT</code>的临时目录作为文件上传的临时目录,但是该目录会在<code>10</code>天之后被系统自动清理掉 !`</em></li>
</ul>
<p><em>继而程序抛出如下异常信息 :</em></p>
<div class="hljs"><pre><code class="hljs groovy">java.io.<span class="hljs-string">IOException:</span> java.io.<span class="hljs-string">FileNotFoundException:</span>
<span class="hljs-regexp">/tmp/</span>tomcat<span class="hljs-number">.273391201583741210</span><span class="hljs-number">.8080</span><span class="hljs-regexp">/work/</span>Tomcat<span class="hljs-regexp">/localhost/</span>ROOT<span class="hljs-regexp">/upload/</span>portrait/myportrait.jpg (No such file or directory)...</code></pre></div>


<h3 id="异常分析"><a href="#异常分析" class="headerlink" title="异常分析"></a>异常分析</h3><p><em><code>upload/portrait</code>是我用于存储头像的项目目录,而<code>transferTo(File dest)</code>方法预期写入的文件路径为<code>/tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/upload/portrait/</code>,我们并没有创建该目录,因此会抛出此异常信息 !</em></p>
<h3 id="问题分析"><a href="#问题分析" class="headerlink" title="问题分析"></a>问题分析</h3><p><em>为什么会这样呢 ? 相对路径-预期路径应该是项目路径<code>/tmp/source/</code>,但是报错确是一个系统临时文件路径,由于是写入文件时报错,继而我们来查看一下<code>transferTo(File dest)</code>的源码吧 :</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.apache.catalina.core;
<span class="hljs-comment">//......</span>

<span class="hljs-comment">/**
 * Adaptor to allow &#123;<span class="hljs-doctag">@link</span> FileItem&#125; objects generated by the package renamed
 * commons-upload to be used by the Servlet 3.0 upload API that expects
 * &#123;<span class="hljs-doctag">@link</span> Part&#125;s.
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ApplicationPart</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Part</span> </span>&#123;

    <span class="hljs-comment">//......</span>

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">write</span><span class="hljs-params">(String fileName)</span> <span class="hljs-keyword">throws</span> IOException </span>&#123;
        File file = <span class="hljs-keyword">new</span> File(fileName);
        <span class="hljs-keyword">if</span> (!file.isAbsolute()) &#123;
            file = <span class="hljs-keyword">new</span> File(location, fileName);
        &#125;
        <span class="hljs-keyword">try</span> &#123;
            fileItem.write(file);
        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(e);
        &#125;
    &#125;
&#125;</code></pre></div>

<p><em>由源码可知,在使用<code>Servlet3.0</code>支持的上传文件功能时,若我们没有使用绝对路径,<code>transferTo(File dest)</code>方法会在相对路径前添加一个<code>location</code>路径 ! 继而影响了SpringMVC的<code>MultipartFile</code>的使用 .</em></p>
<h3 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h3><h4 id="使用绝对路径"><a href="#使用绝对路径" class="headerlink" title="使用绝对路径"></a>使用绝对路径</h4><p><em>通过<code>ResourceUtils.getURL(&quot;classpath:&quot;).getPath()</code>获取项目的绝对路径,为防止项目路径中含有空格等特殊字符继而乱码,可以通过<code>URLDecoder.decode(String s, Charset charset)</code>对其进行解码.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">//项目下存储头像的目录</span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String uploadPath = <span class="hljs-string">"/static/upload/friend_portrait/"</span>;
<span class="hljs-comment">//指定存储头像目录的完整路径(项目发布路径):若不使用绝对路径,则Spring boot会默认将上传的文件存储到临时目录中</span>
String dirPath = URLDecoder.decode(ResourceUtils.getURL(<span class="hljs-string">"classpath:"</span>).getPath(), StandardCharsets.UTF_8) + uploadPath;</code></pre></div>

<p><em>注 : 当我们使用<code>ClassLoader()</code>的<code>getResource()</code>方法获取路径时,获取到的路径是已被<code>URLEncoder.encode(path,&quot;utf-8&quot;)</code>编码了的,当路径中存在中文和空格时,它会对这些字符进行转换,继而得到的往往不是我们想要的真实路径,所以我们可以调用<code>URLDecoder.decode(String s, Charset charset)</code>方法进行解码,以便得到原始的中文及空格路径. 发送的时候使用<code>encode(String s, Charset charset)</code>编码,接收的时候使用<code>URLDecoder.decode(String s, Charset charset)</code>解码,按指定的编码格式进行编码、解码,可以保证不会出现乱码哟 ~</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">//使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式</span>
URLEncoder.encode(String s, Charset charset)

<span class="hljs-comment">//使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码</span>
URLDecoder.decode(String s, Charset charset)</code></pre></div>


<h4 id="修改location的值"><a href="#修改location的值" class="headerlink" title="修改location的值"></a>修改location的值</h4><p><em><code>location</code>可以理解为临时文件目录,可以通过配置<code>location</code>的值,使其指向我们的项目路径,继而来解决此问题. 只需在Spring Boot启动类中添如下代码 :</em></p>
<div class="hljs"><pre><code class="hljs java"> <span class="hljs-meta">@Bean</span>
 <span class="hljs-function">MultipartConfigElement <span class="hljs-title">multipartConfigElement</span><span class="hljs-params">()</span> </span>&#123;
    MultipartConfigFactory factory = <span class="hljs-keyword">new</span> MultipartConfigFactory();
    factory.setLocation(<span class="hljs-string">"/app/pttms/tmp"</span>);
    <span class="hljs-keyword">return</span> factory.createMultipartConfig();
&#125;</code></pre></div>



<h3 id="示例程序"><a href="#示例程序" class="headerlink" title="示例程序"></a>示例程序</h3><p><em>该上传文件的示例程序摘自我的Sping Boot-好友备忘录小项目,程序中使用<code>绝对路径</code>方案来解决上述问题,该程序具有参考与学习价值哟~</em></p>
<ol>
<li><p><em>FriendController.java : 好友信息控制器</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.haungyuhui.memo.controller;

<span class="hljs-keyword">import</span> com.github.pagehelper.PageHelper;
<span class="hljs-keyword">import</span> com.github.pagehelper.PageInfo;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;
<span class="hljs-keyword">import</span> org.springframework.util.ResourceUtils;
<span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.*;
<span class="hljs-keyword">import</span> org.springframework.web.multipart.MultipartFile;
<span class="hljs-keyword">import</span> pers.haungyuhui.memo.bean.Friend;
<span class="hljs-keyword">import</span> pers.haungyuhui.memo.service.FriendService;
<span class="hljs-keyword">import</span> pers.haungyuhui.memo.util.UploadFile;

<span class="hljs-keyword">import</span> javax.annotation.Resource;
<span class="hljs-keyword">import</span> java.io.FileNotFoundException;
<span class="hljs-keyword">import</span> java.net.URLDecoder;
<span class="hljs-keyword">import</span> java.nio.charset.StandardCharsets;
<span class="hljs-keyword">import</span> java.util.HashMap;
<span class="hljs-keyword">import</span> java.util.List;
<span class="hljs-keyword">import</span> java.util.Map;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: memo
 * <span class="hljs-doctag">@description</span>: 控制器-管理好友信息页面
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 6/28/2019-8:25 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-meta">@Controller</span>
<span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/memo"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentController</span> </span>&#123;

    <span class="hljs-comment">//......</span>

    <span class="hljs-comment">//项目下存储头像的目录,需放在静态资源'static'目录下哟</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String uploadPath = <span class="hljs-string">"/static/upload/friend_portrait/"</span>;

    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@description</span>: 上传头像-原理:将头像上传到项目发布目录中,通过读取数据库中的头像路径来显示头像
     * <span class="hljs-doctag">@param</span>: photo
     * <span class="hljs-doctag">@param</span>: request
     * <span class="hljs-doctag">@date</span>: 2019-06-29 4:20 PM
     * <span class="hljs-doctag">@return</span>: java.util.Map&lt;java.lang.String, java.lang.Object&gt;
     */</span>
    <span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/uploadPhoto"</span>)
    <span class="hljs-meta">@ResponseBody</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Map&lt;String, Object&gt; <span class="hljs-title">uploadPhoto</span><span class="hljs-params">(MultipartFile photo)</span> <span class="hljs-keyword">throws</span> FileNotFoundException </span>&#123;
        <span class="hljs-comment">//指定存储头像目录的完整路径(项目发布路径): 若不使用绝对路径,则Spring boot会默认将上传的文件存储到临时目录中</span>
        String dirPath = URLDecoder.decode(ResourceUtils.getURL(<span class="hljs-string">"classpath:"</span>).getPath(), StandardCharsets.UTF_8) + uploadPath;
        <span class="hljs-comment">//返回头像的上传结果</span>
        <span class="hljs-keyword">return</span> UploadFile.getUploadResult(photo, dirPath, uploadPath);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>UploadFile.java : 上传文件的工具类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.haungyuhui.memo.util;

<span class="hljs-keyword">import</span> org.apache.commons.io.filefilter.SuffixFileFilter;
<span class="hljs-keyword">import</span> org.springframework.web.multipart.MultipartFile;

<span class="hljs-keyword">import</span> java.io.File;
<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.util.HashMap;
<span class="hljs-keyword">import</span> java.util.Map;
<span class="hljs-keyword">import</span> java.util.UUID;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: memo
 * <span class="hljs-doctag">@description</span>: 上传文件工具类
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 6/29/2019-9:38 AM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UploadFile</span> </span>&#123;

    <span class="hljs-comment">//限制头像大小最大为20M</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MAX_SIZE = <span class="hljs-number">20971520</span>;
    <span class="hljs-comment">//存储文件上传失败的错误信息</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; error_result = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
    <span class="hljs-comment">//存储头像的上传结果信息</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; upload_result = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
    <span class="hljs-comment">//指定上传文件的类型</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String[] suffixs = <span class="hljs-keyword">new</span> String[]&#123;<span class="hljs-string">".png"</span>, <span class="hljs-string">".PNG"</span>, <span class="hljs-string">".jpg"</span>, <span class="hljs-string">".JPG"</span>, <span class="hljs-string">".jpeg"</span>, <span class="hljs-string">".JPEG"</span>, <span class="hljs-string">".gif"</span>, <span class="hljs-string">".GIF"</span>, <span class="hljs-string">".bmp"</span>, <span class="hljs-string">".BMP"</span>&#125;;


    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@description</span>: 效验所上传图片的大小及格式等信息...
     * <span class="hljs-doctag">@param</span>: photo
     * <span class="hljs-doctag">@param</span>: path
     * <span class="hljs-doctag">@date</span>: 2019-06-29 9:40 AM
     * <span class="hljs-doctag">@return</span>: java.util.Map&lt;java.lang.String, java.lang.Object&gt;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; <span class="hljs-title">uploadPhoto</span><span class="hljs-params">(MultipartFile photo, String path)</span> </span>&#123;
        <span class="hljs-comment">//若存储文件的目录路径不存在,则创建该目录</span>
        File filePath = <span class="hljs-keyword">new</span> File(path);
        <span class="hljs-keyword">if</span> (!filePath.exists()) &#123;
            filePath.mkdirs();
        &#125;
        <span class="hljs-comment">//限制上传文件的大小</span>
        <span class="hljs-keyword">if</span> (photo.getSize() &gt; MAX_SIZE) &#123;
            error_result.put(<span class="hljs-string">"success"</span>, <span class="hljs-keyword">false</span>);
            error_result.put(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"上传的图片大小不能超过20M哟!"</span>);
            <span class="hljs-keyword">return</span> error_result;
        &#125;
        <span class="hljs-comment">// 限制上传的文件类型</span>
        SuffixFileFilter suffixFileFilter = <span class="hljs-keyword">new</span> SuffixFileFilter(suffixs);
        <span class="hljs-keyword">if</span> (!suffixFileFilter.accept(<span class="hljs-keyword">new</span> File(path + photo.getOriginalFilename()))) &#123;
            error_result.put(<span class="hljs-string">"success"</span>, <span class="hljs-keyword">false</span>);
            error_result.put(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"禁止上传此类型文件! 请上传图片哟!"</span>);
            <span class="hljs-keyword">return</span> error_result;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
    &#125;

    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@description</span>: 获取头像的上传结果信息
     * <span class="hljs-doctag">@param</span>: photo
     * <span class="hljs-doctag">@param</span>: dirPaht
     * <span class="hljs-doctag">@param</span>: portraitPath
     * <span class="hljs-doctag">@date</span>: 2019-06-29 9:44 AM
     * <span class="hljs-doctag">@return</span>: java.util.Map&lt;java.lang.String, java.lang.Object&gt;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map&lt;String, Object&gt; <span class="hljs-title">getUploadResult</span><span class="hljs-params">(MultipartFile photo, String dirPath, String uploadPath)</span> </span>&#123;

        <span class="hljs-keyword">if</span> (!photo.isEmpty() &amp;&amp; photo.getSize() &gt; <span class="hljs-number">0</span>) &#123;
            <span class="hljs-comment">//效验图片-error_result: 存储头像上传失败的错误信息</span>
            Map&lt;String, Object&gt; error_result = uploadPhoto(photo, dirPath);
            <span class="hljs-keyword">if</span> (error_result != <span class="hljs-keyword">null</span>) &#123;
                <span class="hljs-keyword">return</span> error_result;
            &#125;
            <span class="hljs-comment">//使用UUID重命名图片名称(uuid__原始图片名称)</span>
            String newPhotoName = UUID.randomUUID() + <span class="hljs-string">"__"</span> + photo.getOriginalFilename();
            <span class="hljs-comment">//将上传的图片保存到目标目录下</span>
            <span class="hljs-keyword">try</span> &#123;
                photo.transferTo(<span class="hljs-keyword">new</span> File(dirPath + newPhotoName));
                <span class="hljs-comment">//将存储头像的目录路径返回给页面</span>
                upload_result.put(<span class="hljs-string">"success"</span>, <span class="hljs-keyword">true</span>);
                upload_result.put(<span class="hljs-string">"portrait_path"</span>, uploadPath + newPhotoName);
            &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;
                e.printStackTrace();
                upload_result.put(<span class="hljs-string">"success"</span>, <span class="hljs-keyword">false</span>);
                upload_result.put(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"上传文件失败! 服务器端发生异常!"</span>);
                <span class="hljs-keyword">return</span> upload_result;
            &#125;

        &#125; <span class="hljs-keyword">else</span> &#123;
            upload_result.put(<span class="hljs-string">"success"</span>, <span class="hljs-keyword">false</span>);
            upload_result.put(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"头像上传失败! 未找到指定图片!"</span>);
        &#125;
        <span class="hljs-keyword">return</span> upload_result;
    &#125;
&#125;</code></pre></div></li>
</ol>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Spring-Boot/">Spring Boot</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2019/07/01/Spring-Boot%E6%8B%A5%E6%8A%B1MyBatis%E5%8F%8ARedis/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Spring-Boot拥抱MyBatis及Redis ~</span>
                        <span class="visible-mobile">Previous</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2019/06/30/Spring-Boot%E9%A1%B9%E7%9B%AE-%E5%A5%BD%E5%8F%8B%E5%A4%87%E5%BF%98%E5%BD%95/">
                        <span class="hidden-mobile">Spring Boot项目:好友备忘录</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Spring Boot之文件上传与下载&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
